extends CharacterBody2D
class_name Wizard

signal die

@export var stats: PandoraEntity
@onready var body: Node2D = %body
@onready var shadow: AnimatedSprite2D = $body/shadow
@onready var sprite: AnimatedSprite2D = %sprite
@onready var enemy_health_component: EnemyHealthComponent = %EnemyHealthComponent
@onready var attack_vfx: AnimatedSprite2D = %attack_vfx
@onready var wizard_skill: WizardSkill = %WizardSkill
@onready var animation_player: AnimationPlayer = %AnimationPlayer

var wizard_stats: PandoraEntity
var target_player:Player:
	get:
		return Utils.player
		
func flip_h(flip: bool):
	if flip:
		body.scale.x = -1
	else:
		body.scale.x = 1

func _ready():
	wizard_stats = stats.instantiate()
	enemy_health_component.on_first_half_hp.connect(_on_half_health)
	enemy_health_component.die.connect(on_die)
	# 添加到敌人组
	add_to_group("enemy")
	
func _on_half_health():
	wizard_stats.set_float("anim_speed", 2.0)
	wizard_stats.set_float("attack_cooldown", 0.2)
	wizard_stats.set_bool("is_half_health", true)

func find_player_and_flip_h():
	if target_player != null:
		flip_h(global_position.direction_to(target_player.global_position).x < 0)

func on_die() -> void:
	die.emit()

func get_stats():
	return wizard_stats
